"""
五子棋游戏数据架构模块

定义与五子棋游戏相关的数据架构，用于API请求和响应的数据验证。
"""

from datetime import datetime
from typing import List, Optional, Dict, Any, Union

from pydantic import BaseModel, Field


class GameMoveBase(BaseModel):
    """游戏动作基础模型"""
    x: int = Field(..., description="横坐标", ge=0)
    y: int = Field(..., description="纵坐标", ge=0)


class GameMoveCreate(GameMoveBase):
    """创建游戏动作的请求模型"""
    pass


class GameMove(GameMoveBase):
    """游戏动作响应模型"""
    id: int
    game_id: int
    user_id: int
    color: str
    move_number: int
    created_at: datetime
    
    class Config:
        orm_mode = True


class PlayerInfo(BaseModel):
    """玩家信息模型"""
    id: int
    username: str
    color: Optional[str] = None
    rating: Optional[int] = None


class GameBase(BaseModel):
    """游戏基础模型"""
    board_size: int = Field(15, description="棋盘大小")


class GameCreate(GameBase):
    """创建游戏的请求模型"""
    pass


class GameUpdate(BaseModel):
    """更新游戏的请求模型"""
    player2_id: Optional[int] = None
    status: Optional[str] = None
    winner_id: Optional[int] = None
    is_draw: Optional[bool] = None


class GameInDB(GameBase):
    """数据库中的游戏模型"""
    id: int
    board_state: str
    status: str
    player1_id: int
    player2_id: Optional[int] = None
    player1_color: Optional[str] = None
    player2_color: Optional[str] = None
    current_turn: Optional[str] = None
    winner_id: Optional[int] = None
    is_draw: bool
    started_at: Optional[datetime] = None
    ended_at: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime
    
    class Config:
        orm_mode = True


class GameOut(BaseModel):
    """游戏响应模型"""
    id: int
    status: str
    board_size: int
    board: List[List[Optional[str]]]
    last_move: Optional[Dict[str, Any]] = None
    current_turn: Optional[str] = None
    player1: PlayerInfo
    player2: Optional[PlayerInfo] = None
    winner: Optional[Dict[str, Any]] = None
    moves: List[GameMove] = []
    started_at: Optional[datetime] = None
    created_at: datetime
    
    class Config:
        orm_mode = True


class GameListItem(BaseModel):
    """游戏列表项响应模型"""
    id: int
    status: str
    player1: PlayerInfo
    player2: Optional[PlayerInfo] = None
    winner_id: Optional[int] = None
    started_at: Optional[datetime] = None
    created_at: datetime
    
    class Config:
        orm_mode = True 